package com.dragon.logical.service.auth.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dragon.repository.entity.Mod;
import com.dragon.repository.entity.ModRole;
import com.dragon.repository.mapper.ModMapper;
import com.dragon.logical.service.auth.IModService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.repository.mapper.ModRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 模块表 服务实现类
 * </p>
 *
 * @author sy
 * @since 2023-07-20
 */
@Service
public class ModServiceImpl extends ServiceImpl<ModMapper, Mod> implements IModService {

    @Autowired
    ModMapper modMapper;
    @Autowired
    ModRoleMapper modRoleMapper;

    @Override
    public List<Mod> modList() {
        List<Mod> mods = modMapper.selectList(null);
        return mods;
    }

    @Override
    public void modAdd(Mod mod) {
        modMapper.insert(mod);
    }

    @Override
    public void modUpdate(Mod mod) {
        modMapper.updateById(mod);
    }

    @Override
    public void modDelete(Integer id) {
        List<ModRole> modRoles = modRoleMapper.selectList(new QueryWrapper<ModRole>()
                .lambda().eq(ModRole::getModId, id));
        for (ModRole modRole : modRoles) {
            modRoleMapper.deleteById(modRole);
        }
        modMapper.deleteById(id);
    }
}
